package jdbcTest;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.sun.corba.se.spi.orbutil.fsm.Guard.Result;

public class StudentDao {
    private Connection conn;
    public boolean addStudent(Student stu) throws SQLException,ClassNotFoundException{
    	if (conn == null) {
			conn = ConnectionUtil.getConnection();
		}
    	Statement stmt =conn.createStatement();
    	String sqlString = "insert into student values('"+stu.getXm()+"','"+stu.getXb()+"','"+stu.getTel()+"')";
         int i = stmt.executeUpdate(sqlString);
         return i > 0;
    }
    
    
    
    public boolean delStudent(Student stu) throws SQLException,ClassNotFoundException{
    	if (conn == null) {
			conn = ConnectionUtil.getConnection();
		}
    	Statement stmt =conn.createStatement();
    	String sqlString = "delete from student where xm='"+stu.getXm()+"'";
         int i = stmt.executeUpdate(sqlString);
         return i > 0;
    }
    
    
    public boolean updateStudent(Student stu) throws SQLException,ClassNotFoundException{
    	if (conn == null) {
			conn = ConnectionUtil.getConnection();
		}
    	Statement stmt =conn.createStatement();
    	String sqlString = "update student set xm='"+stu.getXm()+"',xb='"+stu.getXb()+"'";
         int i = stmt.executeUpdate(sqlString);
         return i > 0;
    }
    
    public ArrayList<Student> listAStudent() throws SQLException,ClassNotFoundException{
    	if (conn == null) {
			conn = ConnectionUtil.getConnection();
		}
    	Statement stmt = conn.createStatement();
    	ResultSet rs = stmt.executeQuery("select * from student;");
    	ArrayList<Student> list = new ArrayList<Student>();
    	while(rs.next()){
    		Student student = new Student();
    		student.setXm(rs.getString("xm"));
    		student.setXm(rs.getString("xb"));
    		student.setXm(rs.getString("tel"));
    		list.add(student);
    	}
    	return list;   
    }
}
